package com.lehecai.core.warning.impl;

import com.lehecai.core.service.queue.JmsQueueService;
import com.lehecai.core.warning.WarningMessage;
import com.lehecai.core.warning.WarningMethod;
import com.lehecai.core.warning.WarningType;
import org.apache.commons.lang.StringUtils;

import javax.jms.Queue;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by qatang on 14-7-23.
 */
public class WarningMessageToolImpl extends LeheQWarningToolImpl {
    private JmsQueueService jmsQueueService;

    private Queue warningMessageQueue;

    @Override
    public void sendSMS(WarningType warningType, String message) {
        if (getSmsContacts() == null) {
            logger.error("短信联系人未设置");
            return;
        }

        this.sendSMS(warningType, message, StringUtils.join(getSmsContacts(), ","));
    }

    @Override
    public void sendMail(WarningType warningType, String message) {
        if (getMailContacts() == null) {
            logger.error("邮件联系人未设置");
            return;
        }
        this.sendMail(warningType, message, StringUtils.join(getMailContacts(), ","));
    }

    @Override
    public void sendMail(WarningType warningType, String message, String contactStr) {
        if (isDisableWarning()) {
            return;
        }

        if (contactStr == null) {
            this.sendMail(warningType, message);
            return;
        }

        WarningMessage warningMessage = new WarningMessage();
        warningMessage.setWarningType(warningType);
        warningMessage.setContent(message);
        warningMessage.setTimestamp(new Date());
        warningMessage.setPriority(0);
        warningMessage.setWarningMethod(WarningMethod.MAIL);

        List<String> contactList = Arrays.asList(StringUtils.split(contactStr, ","));
        warningMessage.setReceivers(contactList);
        this.sendMessage(warningMessage);
    }

    @Override
    public void sendSMS(WarningType warningType, String message, String contactStr) {
        if (isDisableWarning()) {
            return;
        }

        if (contactStr == null) {
            this.sendSMS(warningType, message);
            return;
        }

        WarningMessage warningMessage = new WarningMessage();
        warningMessage.setWarningType(warningType);
        warningMessage.setContent(message);
        warningMessage.setTimestamp(new Date());
        warningMessage.setPriority(0);
        warningMessage.setWarningMethod(WarningMethod.SMS);

        List<String> contactList = Arrays.asList(StringUtils.split(contactStr, ","));
        warningMessage.setReceivers(contactList);
        this.sendMessage(warningMessage);
    }

    @Override
    public void sendMail(String subject, String message, String contactStr) {
        if (isDisableWarning()) {
            return;
        }

        if (this.isDevelopMode()) {
            // 如果测试环境开启，拦截所有内容，均发送到默认测试环境的联系人
            if (this.getDevelopMailContacts() == null || this.getDevelopMailContacts().isEmpty()) {
                // 未配置则不做任何处理
                return;
            }
            // 替换收件人
            contactStr = StringUtils.join(this.getDevelopMailContacts(), ",");
        }

        if (contactStr == null) {
            logger.error("邮件联系人未设置");
            return;
        }

        String[] contactArray = contactStr.split(",");
        if (contactArray.length == 0) {
            logger.error("邮件联系人未设置");
            return;
        }

        WarningMessage warningMessage = new WarningMessage();
        warningMessage.setSubject(subject);
        warningMessage.setContent(message);
        warningMessage.setTimestamp(new Date());
        warningMessage.setPriority(0);
        warningMessage.setWarningMethod(WarningMethod.MAIL);

        List<String> contactList = Arrays.asList(StringUtils.split(contactStr, ","));
        warningMessage.setReceivers(contactList);
        this.sendMessage(warningMessage);
    }

    @Override
    public void sendSMS(String message, String contactStr) {
        if (isDisableWarning()) {
            return;
        }

        if (this.isDevelopMode()) {
            // 如果测试环境开启，拦截所有内容，均发送到默认测试环境的联系人
            if (this.getDevelopSmsContacts() == null || this.getDevelopSmsContacts().isEmpty()) {
                // 未配置则不做任何处理
                return;
            }
            // 替换收件人
            contactStr = StringUtils.join(this.getDevelopSmsContacts(), ",");
        }

        if (contactStr == null) {
            logger.error("短信联系人未设置");
            return;
        }

        String[] contactArray = contactStr.split(",");
        if (contactArray.length == 0) {
            logger.error("短信联系人未设置");
            return;
        }

        WarningMessage warningMessage = new WarningMessage();
        warningMessage.setContent(message);
        warningMessage.setTimestamp(new Date());
        warningMessage.setPriority(0);
        warningMessage.setWarningMethod(WarningMethod.SMS);

        List<String> contactList = Arrays.asList(StringUtils.split(contactStr, ","));
        warningMessage.setReceivers(contactList);
        this.sendMessage(warningMessage);
    }

    @Override
    public void sendMessage(WarningMessage warningMessage) {
        try {
            this.getJmsQueueService().send(this.getWarningMessageQueue(), warningMessage);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void setJmsQueueService(JmsQueueService jmsQueueService) {
        this.jmsQueueService = jmsQueueService;
    }

    public JmsQueueService getJmsQueueService() {
        return jmsQueueService;
    }

    public Queue getWarningMessageQueue() {
        return warningMessageQueue;
    }

    public void setWarningMessageQueue(Queue warningMessageQueue) {
        this.warningMessageQueue = warningMessageQueue;
    }
}
